#!/bin/bash

echo "===== DVHealth Docker配置检查脚本 ====="
echo

# 检查Docker和Docker Compose版本
echo "1. 检查Docker和Docker Compose版本..."
docker --version || echo "⚠️ Docker未安装或不可用"
docker compose version || echo "⚠️ Docker Compose未安装或不可用"
echo

# 检查目录结构
echo "2. 检查项目目录结构..."
if [ -d "./backend" ] && [ -d "./frontend" ] && [ -d "./nginx" ]; then
  echo "✅ 项目目录结构正确"
else
  echo "❌ 项目目录结构不正确，缺少必要目录"
  echo "   需要存在的目录: backend/, frontend/, nginx/"
fi
echo

# 检查配置文件
echo "3. 检查配置文件..."
CONFIG_FILES=("backend/.env.prod" "docker-compose.yml" "nginx/nginx.conf")
for file in "${CONFIG_FILES[@]}"; do
  if [ -f "$file" ]; then
    echo "✅ $file 存在"
  else
    echo "❌ $file 不存在"
  fi
done
echo

# 检查Dockerfile
echo "4. 检查Dockerfile..."
DOCKERFILES=("backend/Dockerfile" "frontend/Dockerfile")
for file in "${DOCKERFILES[@]}"; do
  if [ -f "$file" ]; then
    echo "✅ $file 存在"
  else
    echo "❌ $file 不存在"
  fi
done
echo

# 检查启动脚本
echo "5. 检查启动脚本..."
if [ -f "backend/entrypoint.sh" ]; then
  echo "✅ backend/entrypoint.sh 存在"
  if [ -x "backend/entrypoint.sh" ]; then
    echo "✅ backend/entrypoint.sh 有执行权限"
  else
    echo "⚠️ backend/entrypoint.sh 缺少执行权限"
    echo "   请运行: chmod +x backend/entrypoint.sh"
  fi
else
  echo "❌ backend/entrypoint.sh 不存在"
fi
echo

# 检查端口占用（Linux/Mac）
if [ "$(uname)" != "Windows_NT" ]; then
  echo "6. 检查端口占用情况..."
  PORTS=(80 8000 3306 6379 8080)
  for port in "${PORTS[@]}"; do
    if lsof -Pi :$port -sTCP:LISTEN -t >/dev/null ; then
      echo "⚠️  端口 $port 已被占用，请检查并释放该端口"
    else
      echo "✅  端口 $port 可用"
    fi
  done
  echo
fi

# 检查docker-compose.yml语法
echo "7. 检查docker-compose.yml语法..."
docker compose config -q
if [ $? -eq 0 ]; then
  echo "✅ docker-compose.yml 语法正确"
else
  echo "❌ docker-compose.yml 语法错误"
fi
echo

echo "===== 配置检查完成 ====="
echo "请修复任何检测到的问题，然后运行以下命令进行部署："
echo "  docker-compose up --build -d"
echo